package com.xja.controller;/**
 * @className: ArticleController
 * @description: TODO 类描述
 * @author: wkx
 * @date: 2022/4/17
 **/

import com.xja.pojo.Admin;
import com.xja.pojo.Article;
import com.xja.pojo.ArticleCheckMessage;
import com.xja.pojo.BlogUsers;
import com.xja.service.ArticleService;
import com.xja.service.UserService;
import org.apache.dubbo.config.annotation.Reference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName : ArticleController  //类名

 * @Author : Lenovo //作者
 * @Date: 2022/4/17  23:09
 */
@Controller
@RequestMapping("article")
public class ArticleController {
    private Logger logger = LoggerFactory.getLogger(ArticleController.class);
    @Reference(url = "dubbo://localhost:20883")
    private ArticleService articleService;
    @Reference(url = "dubbo://localhost:20881")
    private UserService userService;

    @RequestMapping("getInitCheck")
    public String getInitCheck(HttpSession session, Model model,@RequestParam(value = "page",defaultValue = "1") Integer page){
        logger.info("获取初审核文章列表");
        Admin admin = (Admin) session.getAttribute("user");
        List<Article> articleList = articleService.getInitCheckArticle(admin.getAdminId(),page);

        Map<Integer,BlogUsers> map = new HashMap<>();
        for (Article article:articleList){
            BlogUsers blogUsers = userService.selectUserById(article.getBloguid());
            map.put(article.getAid(),blogUsers);
        }
        model.addAttribute("articleList",articleList);
        model.addAttribute("map",map);
        return "checkInitArticle";
    }

    @RequestMapping("getCheck")
    public String getCheck(HttpSession session, Model model,@RequestParam(value = "page",defaultValue = "1") Integer page){
        logger.info("获取复审核文章列表");
        Admin admin = (Admin) session.getAttribute("user");
        List<Article> articleList = articleService.getCheckArticle(page);

        Map<Integer,BlogUsers> map = new HashMap<>();
        for (Article article:articleList){
            BlogUsers blogUsers = userService.selectUserById(article.getBloguid());
            map.put(article.getAid(),blogUsers);
        }
        model.addAttribute("articleList",articleList);

        model.addAttribute("map",map);

        return "checkArticle";
    }

    @RequestMapping("toInitCheck")
    public String toInitCheck(Integer aid,Model model){
        logger.info("跳转初审核文章页面");
        Article article = articleService.getArticleByAid(aid);
        model.addAttribute("article",article);

        return "initCheck";
    }
    @RequestMapping("toReCheck")
    public String toReCheck(Integer aid,Model model){
        logger.info("跳转复审核文章页面");
        Article article = articleService.getArticleByAid(aid);
        List<ArticleCheckMessage> message = articleService.getCheckMessage(aid);
        model.addAttribute("article",article);
        model.addAttribute("message",message.get(0));

        return "reCheck";
    }
    @RequestMapping("initCheck")
    public String initCheck(ArticleCheckMessage message){
        logger.info("初审核");
        articleService.addArticleCheckMessage(message);

        return "redirect:/article/getInitCheck";
    }
    @RequestMapping("reCheck")
    public String reCheck(Integer aid,String astatus){
        logger.info("复审核");
        articleService.updateArticlePermit(aid,astatus);

        return "redirect:/article/getCheck";
    }
}
